package cn.gailvlun.gll.presentation.hotel

import android.content.Context
import android.content.Intent
import android.databinding.DataBindingUtil
import android.databinding.ObservableBoolean
import android.os.Bundle

import android.view.MotionEvent
import android.view.View
import cn.gailvlun.gll.R
import cn.gailvlun.gll.databinding.ActivityDayHotelBinding
import cn.gailvlun.gll.presentation.base.BaseActivity
import cn.gailvlun.gll.presentation.hotel.compose.ComposePanelWindow
import cn.gailvlun.gll.presentation.hotel.compose.ComposeState
import cn.gailvlun.gll.presentation.hotel.day.HotelSpeakActivity
import cn.gailvlun.gll.presentation.hotel.day.HotelWriteActivity
import cn.gailvlun.gll.presentation.hotel.drawer.HotelMyShareActivity



class DayHotelActivity : BaseActivity() {

    companion object {
        fun openActivity(context: Context) {
            val intent = Intent(context, DayHotelActivity::class.java)
            context.startActivity(intent)
        }
    }

    private lateinit var mBinding: ActivityDayHotelBinding
    private lateinit var mPresenter: Presenter
    private lateinit var mComposePanelWindow: ComposePanelWindow


    override fun initComponent() {
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_day_hotel)
        mPresenter = Presenter()
        mBinding.presenter = mPresenter
        mComposePanelWindow = ComposePanelWindow(mContext)
    }

    override fun createHandler() {
        mBinding.nbv.setOnBackListener {
            onBackPressed()
        }

        mBinding.nbv.setOnMenuClickListener {
            HotelMyShareActivity.openActivity(mContext)
        }
    }

    override fun loadData(savedInstanceState: Bundle?) {

    }

    inner class Presenter {
        var isComposeVisible = ObservableBoolean(true)
        fun onSpeak() {
            HotelSpeakActivity.openActivity(mContext)
        }

        fun onWrite() {
            HotelWriteActivity.openActivity(mContext)
        }

        fun onEdit(view: View) {
            switchComposePanel(ComposeState.TO_OPEN)
        }
    }

    override fun dispatchTouchEvent(ev: MotionEvent): Boolean {
        return if (switchComposePanel(ComposeState.TO_CLOSE)) {
            true
        } else {
            super.dispatchTouchEvent(ev)
        }
    }

    override fun onPause() {
        super.onPause()
        switchComposePanel(ComposeState.TO_CLOSE)

    }

    private fun switchComposePanel(state: ComposeState): Boolean {
        when (state) {
            ComposeState.TO_CLOSE -> {

                return if (mComposePanelWindow.isShowing) {
                    mComposePanelWindow.dismiss()
                    mPresenter.isComposeVisible.set(true)
                    true
                } else {
                    false
                }
            }
            ComposeState.TO_OPEN -> {
                return if (!mComposePanelWindow.isShowing) {
                    mComposePanelWindow.show()
                    mPresenter.isComposeVisible.set(false)
                    true
                } else {
                    false
                }
            }

        }
    }
}
